home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Utilities / Catharsis / Catharsis_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  10.9 KB  |  374 lines

  1. /* 
  2.    Carthasis Library
  3.  
  4.    Author:    Jörg van de Loo
  5.             Hövel 15
  6.             47559 Kranenburg
  7.             FRG
  8.  
  9.    using MAXON's C++ 4 in C mode.
  10.  
  11.    USE LARGE DATA MODEL TO COMPILE THIS FILE
  12.    AND LINK WITHOUT STARTUP-CODE.
  13.  
  14.    NOTE: A library has to be reentrant, which means that it's not allowed to use
  15.    global variables that can be modified at the same time by different
  16.    callers. Thus you have to make them local, e.g. a good thing is to use
  17.    local stack variables and of course AllocMem() allocated structures or
  18.    equivalent allocated structures within the routine which is executed!
  19. */
  20.  
  21. #include    <exec/nodes.h>
  22. #include    <exec/memory.h>
  23. #include    <exec/lists.h>
  24. #include    <exec/libraries.h>
  25. #include    <exec/resident.h>
  26. #include    <exec/execbase.h>
  27.  
  28. #include    <libraries/dos.h>
  29. #include    <dos/dosextens.h>
  30.  
  31. #include    <graphics/gfxbase.h>
  32.  
  33. #include    <intuition/intuitionbase.h>
  34.  
  35. #include    <utility/tagitem.h>
  36.  
  37. #include    <clib/exec_protos.h>
  38. #include    <pragma/exec_lib.h>
  39.  
  40. #include    <clib/dos_protos.h>
  41. #include    <pragma/dos_lib.h>
  42.  
  43. #include    <clib/graphics_protos.h>
  44. #include    <pragma/graphics_lib.h>
  45.  
  46. #include    <clib/wb_protos.h>
  47. #include    <pragma/wb_lib.h>
  48.  
  49. #include    <clib/icon_protos.h>
  50. #include    <pragma/icon_lib.h>
  51.  
  52.  
  53. #ifdef __GNUC__
  54. #define ASM
  55. #define REG(reg,arg) arg __asm(#reg)
  56. #else
  57. #if !defined (__MAXON__)
  58. #define ASM __asm
  59. #endif
  60. #define REG(reg,arg) register __##reg arg
  61. #endif
  62.  
  63. #ifdef __MAXON__
  64. extern "C" void GetBaseReg(void);
  65. #define __saveds
  66. #define ASM
  67. #endif
  68.  
  69. #if defined(__MAXON__) || defined(__STORM__)
  70. #define __inline
  71. #define __stdargs
  72. #endif
  73.  
  74. /* ###########################################################
  75.  
  76.    P R O T O T Y P E S
  77. */
  78.  
  79. struct SharedLib *LIB_Open(REG(a6, struct SharedLib *base));
  80. int LIB_Close( REG(a6, struct SharedLib *base));
  81. BPTR LIB_Expunge( REG(a6, struct SharedLib *base));
  82. int LIB_Reserved( REG(a6, struct SharedLib *base));
  83.  
  84.  
  85. int User_Startup( REG(a6, struct SharedLib *base));
  86. void User_Cleanup( REG(a6, struct SharedLib *base));
  87.  
  88. // Up from here thre library functions (prototypes)
  89.  
  90. void ModifyCharacter( struct FontDescription *fd, REG(a6, struct SharedLib *base));
  91. void ModifyCharacterA( REG(a0, struct FontDescription *fd), REG(a6, struct SharedLib *base));
  92. struct TextFont *CreateCatharsisFont( struct TextFont *font, REG(a6, struct SharedLib *base));
  93. struct TextFont *CreateCatharsisFontA( REG(a0, struct TextFont *font), REG(a6, struct SharedLib *base));
  94. struct TextFont *OpenCatharsisFont( struct TextFont *font, REG(a6, struct SharedLib *base));
  95. struct TextFont *OpenCatharsisFontA( REG(a0, struct TextFont *font), REG(a6, struct SharedLib *base));
  96. void CloseCatharsisFont( struct TextFont *font, REG(a6, struct SharedLib *base));
  97. void CloseCatharsisFontA( REG(a0, struct TextFont *font), REG(a6, struct SharedLib *base));
  98. int ComputeCatharsisValue( int bg, int fg, int percent, int normal, REG(a6, struct SharedLib *base));
  99. int ComputeCatharsisValueA( REG(d0, int bg), REG(d1, int fg), REG(d2, int percent), REG(d3, int normal), REG(a6, struct SharedLib *base));
  100. ULONG CompressColorValue( ULONG r, ULONG g, ULONG b, REG(a6, struct SharedLib *base));
  101. ULONG CompressColorValueA( REG(d0, ULONG r), REG(d1, ULONG g), REG(d2, ULONG b), REG(a6, struct SharedLib *base));
  102. ULONG CompatibleColorValue( ULONG v, REG(a6, struct SharedLib *base));
  103. ULONG CompatibleColorValueA( REG(d0, ULONG v), REG(a6, struct SharedLib *base));
  104. ULONG CatharsisColorValue( ULONG val1, ULONG val2, ULONG * red, ULONG * green, ULONG * blue, REG(a6, struct SharedLib *base));
  105. ULONG CatharsisColorValueA( REG(d0, ULONG val1), REG(d1, ULONG val2), REG(a0, ULONG * red), REG(a1, ULONG * green), REG(a2, ULONG * blue), REG(a6, struct SharedLib *base));
  106. void CatharsisText( struct RastPort *rp, unsigned char *str, unsigned int len, unsigned int pen, struct TextFont *font, REG(a6, struct SharedLib *base));
  107. void CatharsisTextA( REG(a1, struct RastPort *rp), REG(a0, unsigned char *str), REG(d0, unsigned int len), REG(d1, unsigned int pen), REG(a2, struct TextFont *font), REG(a6, struct SharedLib *base));
  108.  
  109.  
  110.  
  111. /* ########################################################### */
  112.  
  113. struct ExecBase *SysBase;                    // Global, in data area of library so that the compiler has
  114. struct GfxBase *GfxBase;                    // access to it
  115. struct IntuitionBase *IntuitionBase;
  116.  
  117. UBYTE GrafName[]         = "graphics.library";
  118. UBYTE IntuitionName[] = "intuition.library";
  119.  
  120. #define LIB_Version        1                    // Change these four for your own library...
  121. #define LIB_Revision    0                    // and also in 'LIB_Version.s' !!!
  122.  
  123. UBYTE LIB_Name[] = "catharsis.library";        // Name of this library
  124. UBYTE LIB_IdString[] = "catharsis 1.0p (04-Jul-01)";    // More datails of library (string)
  125.  
  126. /* ########################################################### */
  127.  
  128. /* The library base structure */
  129.  
  130. struct SharedLib
  131. {
  132.     struct Node sl_LibNode;
  133.     UBYTE sl_Flags;                            // LIB Flags
  134.     UBYTE sl_Pad0;                            // Dummy
  135.     UWORD sl_NegSize;                        // LIB Negative size (function table)
  136.     UWORD sl_PosSize;                        // LIB Positive size (it's this table)
  137.     UWORD sl_Version;                        // LIB Version
  138.     UWORD sl_Revision;                        // LIB Revision
  139.     char *sl_IdString;                        // LIB Detail string
  140.     ULONG sl_Sum;                            // LIB Checksum
  141.     UWORD sl_OpenCnt;                        // LIB Open count
  142.     UWORD sl_PatchCnt;                        // - Internally used
  143.     BPTR sl_SegList;                        // LIB Segment pointer
  144.     void *sl_OldFuncPtr;
  145.     struct ExecBase *sl_SysBase;            // Pointers to SysBase
  146.     struct GfxBase *sl_GfxBase;
  147.     struct IntuitionBase *sl_IntuitionBase;
  148.     struct TextFont *sl_TextFont[256];        // Remembered TextFont pointers
  149.     struct TextFont *sl_CatharsisFont[256];    // Created TextFont pointers
  150. };                                            // Size of table (positive size)
  151.  
  152. struct SharedLib *ThisLib;
  153.  
  154. /* ########################################################### */
  155.  
  156. /* M U S T   B E   T H E   V E R Y   F I R S T   E N T R Y
  157.  
  158.    Now, it's in LIB_Resident.s ....
  159.  
  160.    int _INIT_0_lib_start_not_allowed()  // If we are called from CLI
  161.    {
  162.        return -1;
  163.    }                                    // ...exit quick */
  164.  
  165. /* ########################################################### */
  166.  
  167.  
  168. struct Library *LIB_InitRoutine(REG(d0, struct SharedLib *obj), REG(a0, BPTR seglist), REG(a6, struct ExecBase *AbsSysBase))
  169. {
  170.  
  171.     if (!(obj->sl_SysBase == AbsSysBase))    // Library already set up?
  172.     {
  173.         ThisLib = obj;                                // Pointer to own library, global
  174.         obj->sl_SegList = seglist;                    // Remember segment(s) of library
  175.         obj->sl_SysBase = SysBase = AbsSysBase;        // Setup SysBase
  176.         obj->sl_LibNode.ln_Type = NT_LIBRARY;        // Initialize the library...
  177.         obj->sl_LibNode.ln_Name = LIB_Name;
  178.         obj->sl_Flags = LIBF_SUMUSED | LIBF_CHANGED;
  179.         obj->sl_Version = LIB_Version;
  180.         obj->sl_Revision = LIB_Revision;
  181.         obj->sl_IdString = LIB_IdString;
  182.  
  183.         // Call user's startup-routine
  184.         if ( !User_Startup( obj))
  185.         {
  186.             return 0;                                // User init failed, thus creating the library failed
  187.         }
  188.     }
  189.     return (struct Library *) obj;                    // Success!
  190.  
  191. }
  192.  
  193. /* ########################################################### */
  194.  
  195. /* Add here the names of your functions in the right order! */
  196.  
  197. void *LIB_FunctionTable[] =
  198. {
  199.     LIB_Open,
  200.     LIB_Close,
  201.     LIB_Expunge,
  202.     LIB_Reserved,
  203.  
  204. // - Public functions up from here
  205.     ModifyCharacter,
  206.     ModifyCharacterA,
  207.     CreateCatharsisFont,
  208.     CreateCatharsisFontA,
  209.     OpenCatharsisFont,
  210.     OpenCatharsisFontA,
  211.     CloseCatharsisFont,
  212.     CloseCatharsisFontA,
  213.     ComputeCatharsisValue,
  214.     ComputeCatharsisValueA,
  215.     CompressColorValue,
  216.     CompressColorValueA,
  217.     CompatibleColorValue,
  218.     CompatibleColorValueA,
  219.     CatharsisColorValue,
  220.     CatharsisColorValueA,
  221.     CatharsisText,
  222.     CatharsisTextA,
  223.  
  224. // - end mark of function table
  225.     (void *) -1
  226. };
  227.  
  228. /* ########################################################### */
  229.  
  230. /* The APTR is needed because Commodore made a stupid APTR assign instead
  231.    of a useable structure, which could look like this:
  232.  
  233.    struct LibraryInitTable
  234.    {
  235.    unsigned long      LIB_POS_SIZE;
  236.    void             **LIB_FUNC_TABLE;
  237.    void              *LIB_DATA_TABLE;
  238.    void             (*LIB_INIT_FUNCTION)( struct SharedLib *, BPTR, struct ExecBase *);
  239.    };
  240. */
  241.  
  242. const APTR LIB_InitTable[] =
  243. {
  244.     (APTR) sizeof (struct SharedLib),
  245.     LIB_FunctionTable,
  246.     0,                                        // We have to do it in main, since the INIT macros don't deal with MCPP!
  247.      LIB_InitRoutine
  248. };
  249.  
  250.  
  251. /* This has been moved to LIB_Resident.s ....
  252.  
  253.    const struct Resident LIB_Resident =
  254.    {
  255.    RTC_MATCHWORD,                Code of resident (for exec's MakeLibrary())
  256.    (APTR) &LIB_Resident,        Point back to address of begin
  257.    (APTR) ((char *) &LIB_Resident + sizeof( struct Resident)),  End of this structure
  258.    RTF_AUTOINIT,                Flag for auto call
  259.    LIB_Version,                    ...
  260.    NT_LIBRARY,                    Type = library
  261.    0,                            Priority, always zero
  262.    LIB_Name,                    Name of this library
  263.    LIB_IdString,                More datails of library (string)
  264.    (APTR) LIB_InitTable,        Pointer to init-table
  265.    };
  266. */
  267.  
  268. extern const struct Resident LIB_Resident;
  269.  
  270.  
  271. /* ########################################################### */
  272.  
  273. struct SharedLib *LIB_Open( REG(a6, struct SharedLib *obj))
  274. {
  275.     obj->sl_OpenCnt ++;                    // Another caller
  276.  
  277.     obj->sl_Flags &= ~LIBF_DELEXP;        // Prevent from being flushed from memory
  278.  
  279.     return obj;
  280. }
  281.  
  282. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  283.  
  284. int LIB_Close( REG( a6, struct SharedLib *obj))
  285. {
  286.     obj->sl_OpenCnt --;
  287.  
  288.     if (obj->sl_OpenCnt > 0)
  289.         return 0;
  290.  
  291.     if (obj->sl_Flags & LIBF_DELEXP)
  292.         return LIB_Expunge(obj);
  293.     else
  294.         return 0;
  295. }
  296.  
  297. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  298.  
  299. BPTR LIB_Expunge( REG(a6, struct SharedLib * obj))
  300. {
  301.     BPTR seglist = 0;
  302.     long libsize;
  303.  
  304.     if (obj->sl_PatchCnt)
  305.     {
  306.         obj->sl_Flags &= ~LIBF_DELEXP;        // Prevent from being flushed from memory
  307.  
  308.         return 0;
  309.     }
  310.  
  311.     if (obj->sl_OpenCnt <= 0)
  312.     {
  313.         // Invoke user's cleanup-routine
  314.         User_Cleanup( obj);
  315.  
  316.         // Really expunge: remove library and free memory used by it
  317.         seglist = obj->sl_SegList;
  318.  
  319.         Remove( (struct Node *) obj);
  320.  
  321.         libsize = obj->sl_NegSize + obj->sl_PosSize;    // Calc size
  322.  
  323.         FreeMem( (char *) obj - obj->sl_NegSize, libsize);
  324.     }
  325.     else
  326.     {
  327.         obj->sl_Flags |= LIBF_DELEXP;
  328.     }
  329.  
  330.     // Return NULL or seglist
  331.     return seglist;
  332. }
  333.  
  334. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  335.  
  336. int LIB_Reserved( REG(a6, struct SharedLib *obj))
  337. {
  338.     return 0;
  339. }
  340.  
  341. /* ########################################################### */
  342.  
  343.  
  344. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++
  345.    Here you can initialize your library's environment.
  346.    It's only once called when the library is initialized by the system (MakeLibrary()). */
  347.  
  348. int User_Startup( REG(a6, struct SharedLib *obj))
  349. {
  350.     if ((GfxBase = obj->sl_GfxBase = (struct GfxBase *) OpenLibrary( GrafName, 33)))
  351.     {
  352.         if ((IntuitionBase = obj->sl_IntuitionBase = (struct IntuitionBase *) OpenLibrary( IntuitionName, 33)))
  353.         {
  354.             return TRUE;                    // Success
  355.         }
  356.         CloseLibrary((struct Library *) obj->sl_GfxBase);
  357.         obj->sl_GfxBase = 0;
  358.     }
  359.     return FALSE;
  360. }
  361.  
  362. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++
  363.    Do here the necessary clanups your environment has produced.
  364.    It's only called when the library is moved out off memory. */
  365.  
  366. void User_Cleanup(register __a6 struct SharedLib *obj)
  367. {
  368.     if (obj->sl_IntuitionBase)
  369.         CloseLibrary((struct Library *) obj->sl_IntuitionBase);
  370.  
  371.     if (obj->sl_GfxBase)
  372.         CloseLibrary((struct Library *) obj->sl_GfxBase);
  373. }
  374.